package com.bjsxt.listener;

import com.bjsxt.entity.User;
import com.util.IPUtil;
import com.util.ServletUtil;
import org.apache.log4j.Logger;

import javax.servlet.http.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 苏犇 on 2019/1/6. <br>
 */
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
    Logger logger = Logger.getLogger(this.getClass());
    static Map<String, HttpSession> map = new HashMap<>();

    /**
     * Receives notification that a session has been created.
     *
     * @param se the HttpSessionEvent containing the session
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        String sessionId = se.getSession().getId();
        if (logger.isDebugEnabled())
            logger.debug("A session has been created , which id is :" + sessionId);
    }

    /**
     * Receives notification that a session is about to be invalidated.
     *
     * @param se the HttpSessionEvent containing the session
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        String sessionId = se.getSession().getId();
        if (logger.isDebugEnabled())
            logger.debug("A session has been destroyed , which id is :" + sessionId);
    }

    /**
     * Receives notification that an attribute has been added to a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     *              and the name and value of the attribute that was added
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        HttpSession newSession = event.getSession();
        User user = (User) newSession.getAttribute("user");
        if (user != null) {
            HttpSession oldSession = map.remove(user.getUsername());
            if (oldSession != null) {
                logger.info("old session has been remove, which id is : " + oldSession.getId());
                logger.warn("new user login , which login ip is : " + user.getIPAddr());
                oldSession.removeAttribute("user");
                Map reLogin = new HashMap();
                reLogin.put("isRelogin", true);
                reLogin.put("reLoginIP", user.getIPAddr());
                oldSession.setAttribute("reLogin", reLogin);
            }
            map.put(user.getUsername(), newSession);
        }

    }

    /**
     * Receives notification that an attribute has been removed from a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     *              and the name and value of the attribute that was removed
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        User user = ((User) event.getSession().getAttribute("user"));
        if (user != null) {
            String username = user.getUsername();
            map.remove(username);
        }
    }

    /**
     * Receives notification that an attribute has been replaced in a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     *              and the name and (old) value of the attribute that was replaced
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        User user = (User) event.getSession().getAttribute("user");
        if (user != null) {
            HttpSession oldSession = map.remove(user.getUsername());
            oldSession.removeAttribute("user");
        }
    }
}
